package com.example.qimo;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import com.example.qimo.Diray.DiaryDetail;
import com.example.qimo.Diray.Diray;
import com.example.qimo.alarm.AlarmActivity;
import com.example.qimo.alarm.alarmActivity2;
import com.example.qimo.fragment.dealFragment;
import com.example.qimo.fragment.moreFragment;
import com.example.qimo.fragment.myFragment;
import com.example.qimo.fragment.nearbyFragment;
import com.example.qimo.tiktok.TiktokIndexActivity;

import java.util.Calendar;

public class base extends AppCompatActivity{
    private ViewPager vpContainer;
    private RadioGroup rgTabMenu;
    private FragmentPagerAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base);
        //初始化控件
        rgTabMenu = findViewById(R.id.rg_tab_menu);
        vpContainer = findViewById(R.id.vp_container);
        //创建适配器
        adapter = new InnerFragmentPagerAdapter(getSupportFragmentManager());
        //将适配器装载到ViewPager控件上
        vpContainer.setAdapter(adapter);
        //实现页面滑动与导航按钮联动效果
        vpContainer.addOnPageChangeListener(new InnerOnPageChangeListener());
        //实现导航按钮与滑动页面联动
        rgTabMenu.setOnCheckedChangeListener(new InnerOnCheckListener());

    }



    private class InnerFragmentPagerAdapter extends FragmentPagerAdapter {

        public InnerFragmentPagerAdapter(@NonNull FragmentManager fm) {
            super(fm);
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            Fragment fragment = null;
            switch (position) {
                case 0:
                    fragment = new dealFragment();
                    break;
                case 1:
                    fragment = new nearbyFragment();
                    break;
                case 2:
                    fragment = new myFragment();
                    break;
                case 3:
                    fragment = new moreFragment();
                    break;
            }
            return fragment;
        }

        @Override
        public int getCount() {
            return 4;
        }
    }

    //页面改变的监听的接口
    private class InnerOnPageChangeListener implements ViewPager.OnPageChangeListener {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
//页面选择改变的时候
            switch (position){
                case 0:
                    rgTabMenu.check(R.id.rb_tab_item_deal);//让团购的单选按钮选中
                    Intent it = new Intent(base.this, Diray.class);
                    startActivity(it);
                    break;
                case 1:
                    rgTabMenu.check(R.id.rb_tab_item_nearby);//让发现的单选按钮选中
                    Intent it1 = new Intent(base.this, AlarmActivity.class);
                    startActivity(it1);
                    break;
                case 2:
                    rgTabMenu.check(R.id.rb_tab_item_my);//让我的的单选按钮选中
                    Intent it2 = new Intent(base.this, TiktokIndexActivity.class);
                    startActivity(it2);
                    break;
                case 3:
                    rgTabMenu.check(R.id.rb_tab_item_more);//让更多的单选按钮选中
                    break;
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }

    private class InnerOnCheckListener implements RadioGroup.OnCheckedChangeListener {

        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int i) {
            switch (i){
                case R.id.rb_tab_item_deal:
                    vpContainer.setCurrentItem(0);
                    break;
                case R.id.rb_tab_item_nearby:
                    vpContainer.setCurrentItem(1);
                    break;
                case R.id.rb_tab_item_my:
                    vpContainer.setCurrentItem(2);
                    break;case R.id.rb_tab_item_more:
                    vpContainer.setCurrentItem(3);
                    break;

            }
        }
    }



}